﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Projektarbete
{
    static class PopupManager
    {
        private static PopupBase currentPopup;

        private static PopupBase emptyPopup;
        private static InventoryPopup inventoryPopup;
        private static ChestPopup chestPopup;
        private static MerchantPopup merchantPopup;

        public static bool isInventoryOpen;
        public static bool isChestPopupOpen;
        public static bool isMerchantOpen;

        public static void Initialize()
        {
            inventoryPopup = new InventoryPopup();
            chestPopup = new ChestPopup();
            emptyPopup = new PopupBase();

            currentPopup = emptyPopup;

            isInventoryOpen = false;
            isChestPopupOpen = false;
            isMerchantOpen = false;
        }

        /// <summary>
        /// To be called every frame
        /// </summary>
        public static void Update()
        {
            currentPopup.Update();
        }

        public static void Draw(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice)
        {
            currentPopup.Draw(spriteBatch, graphicsDevice);
        }

        public static bool IsOverlayOpen()
        {
            return !(currentPopup == emptyPopup);
        }

        public static bool DropItem(Item item)
        {
            Tile facingTile = Player.GetFacingTile();
            Vector2 facingPosition = Vector2.Multiply(Player.GetFacingPositionAsVector2(), 24);

            if (!AIManager.isAIAt(facingPosition))
            {

                if (Collision.TileType(facingTile.getTextureID) == TileTypes.Chest)
                {
                    Player.inventory.RemoveItem(item);
                    facingTile.SetData(new TileData(item, true));
                }
                else
                {
                    Point chestPosition = Player.GetFacingPositionAsPoint();

                    if (Collision.TileType(MapHandler.map[chestPosition.Y][chestPosition.X].getTextureID) == TileTypes.Passable)
                    {
                        Player.inventory.RemoveItem(item);

                        MapHandler.ChangeTile(new Chest(), new Point(chestPosition.X, chestPosition.Y));
                        MapHandler.map[chestPosition.Y][chestPosition.X].SetData(new TileData(item, true)); //true = add, false = remove
                        MapHandler.map[chestPosition.Y][chestPosition.X].UpdateTexture(3, 0);
                    }
                }

                //NET-TODO: Send updated map

                inventoryPopup.UpdateInventoryContents();

                return true;
            }
            else
            {
                HUD.AddNotification(new Notification("Don't throw items at other people", NotificationColor.Info, NotificationLifetime.Info));
                return false;
            }
        }

        public static void OpenCloseInventory()
        {
            if (isChestPopupOpen)
                OpenCloseChest();

            if (isMerchantOpen)
                CloseMerchant();

            if (isInventoryOpen)
            {
                currentPopup = emptyPopup;
                Player.EventMove -= new VoidPointer(OpenCloseInventory);
            }
            else
            {
                Player.EventMove += new VoidPointer(OpenCloseInventory);
                inventoryPopup.UpdateInventoryContents();
                currentPopup = inventoryPopup;
            }

            isInventoryOpen = !isInventoryOpen;
        }

        public static void OpenCloseChest()
        {
            if (isInventoryOpen)
                OpenCloseInventory();

            if (isMerchantOpen)
                CloseMerchant();

            if (isChestPopupOpen)
            {
                currentPopup = emptyPopup;
                Player.EventMove -= new VoidPointer(OpenCloseChest);
            }
            else
            {
                Player.EventMove += new VoidPointer(OpenCloseChest);
                inventoryPopup.UpdateInventoryContents();
                Tile facingTile = Player.GetFacingTile();

                facingTile.UpdateTexture(3, 0);

                chestPopup.UpdateContents();
                currentPopup = chestPopup;
            }

            isChestPopupOpen = !isChestPopupOpen;
        }

        public static void OpenMerchant()
        {
            if (isChestPopupOpen)
                OpenCloseChest();

            if (isChestPopupOpen)
                OpenCloseChest();

            Player.EventMove += new VoidPointer(CloseMerchant);
            //inventoryOverlay.UpdateContents(Player.inventory);
            //TODO: not use new
            merchantPopup = new MerchantPopup();

            merchantPopup.UpdateContents();

            currentPopup = merchantPopup;

            isMerchantOpen = !isMerchantOpen;
        }

        public static void CloseMerchant()
        {
            currentPopup = emptyPopup;
            Player.EventMove -= new VoidPointer(CloseMerchant);
            isMerchantOpen = false;
        }

        public static void BuyItem(Item item)
        {
            if (Player.gold >= item.getValue)
            {
                if (Player.inventory.AddItem(item))
                {
                    AIManager.RemoveFromMerchantInventory(item, Vector2.Multiply(Player.GetFacingPositionAsVector2(), 24));

                    Player.gold -= item.getValue;
                }
            }
            else
            {
                HUD.AddNotification(new Notification("You do not have enough money to buy that", NotificationColor.Info, NotificationLifetime.Info));
            }
        }

        public static void SellItem(Item item)
        {
            Player.inventory.RemoveItem(item);
            AIManager.AddToMerchantInventory(item, Vector2.Multiply(Player.GetFacingPositionAsVector2(), 24));
            Player.gold += item.getValue;
        }

        /// <summary>
        /// Obsolete, just use DropItem instead
        /// </summary>
        /// <param name="item"></param>
        public static void StoreChestItem(Item item)
        {
            Tile facingTile = Player.GetFacingTile();

            Player.inventory.RemoveItem(item);

            facingTile.SetData(new TileData(item, true));
            facingTile.UpdateTexture(3, 0);

            chestPopup.UpdateContents();
        }

        public static bool TakeChestItem(Item item)
        {
            if (!Player.inventory.AddItem(item))
                return false;

            Tile facingTile = Player.GetFacingTile();

            facingTile.SetData(new TileData(item, false));

            if (facingTile.GetData().linkedListItem.Count == 0)
                facingTile.UpdateTexture(4, 0);

            chestPopup.UpdateContents();
            return true;
        }

        public static void SortInventory(int sortAfter)
        {
            if (sortAfter == 0)
                return;
            else
                Player.inventory.SortBackpack((Inventory.SortOptions)sortAfter - 1);
        }

        public static void SortChest(int sortAfter)
        {
            Tile facingTile = Player.GetFacingTile();

            if (sortAfter == 0)
                return;
            else
                Player.inventory.SortBackpack((Inventory.SortOptions)sortAfter - 1);

            //errorgoeshere!! SortChest???

            //chestPopup.UpdateContents(Player.inventory.GetContentsToInventoryArray());
        }
    }
}
